﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Che.Domain.Entity.Che.User; // 确保你的实体类命名空间正确
using System.Reflection;
using Che.Domain.Entity.trophy;
using Che.Domain.Entity.EventsBut;
using Che.AppService.Dto;
using TR.Domain.Entity.rbac_QXGL;
using SqlSugar;
using Che.Domain.Entity.EPlayers;
using Che.Domain.Entity.ECountry;
using System.Numerics;

namespace Che.EFCore
{
    public class MyDbContext : DbContext
    {
        public string conStr = "Data Source=CHELSEA;Initial Catalog=ChelseaFlootball2;Integrated Security=True;Trust Server Certificate=True;Integrated Security=True;Encrypt=False;";
        public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
        {
            //读取ui的配置信息
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            //输出ef日志
            optionsBuilder.LogTo(msg =>
            {
                if (!msg.Contains("CommandExecuting"))
                {
                    return;
                }
                Console.WriteLine(msg);
            });
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {

            //读取所有的配置类
            modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly);
        }

        // 定义 DbSet 属性
        public DbSet<User> Users { get; set; } // 用户表
        public DbSet<rbac_Roles> rbac_Roles { get; set; } // 角色表
        public DbSet<avatar> avatars { get; set; } // 头像表
        public DbSet<trophyq> trophy { get; set; }//冠军详细表
        public DbSet<Event> Events { get; set; }//赛程表
        public DbSet<Football> Fottball { get; set; }//球队表
        public DbSet<Stadium> Stadium { get; set; }//球场表
        public DbSet<EventType> Eventstype { get; set; }//赛程类型表
        public DbSet<EventDetail> vw_EventDetails { get; set; }//赛事视图
        public DbSet<rbac_UserRoles> rbac_UserRoles { get; set; }//用户角色表


        //sqlsugar创建表
       public void Mains()
        {
            var db = new SqlSugarScope(new ConnectionConfig()
            {
                ConnectionString = "Data Source=CHELSEA;Initial Catalog=ChelseaFlootball2;Integrated Security=True;Trust Server Certificate=True;Encrypt=False;",
                DbType = DbType.SqlServer,
                IsAutoCloseConnection = true
            });

            // 修正1：正确获取程序集类型
            Type[] entityTypes = typeof(Players).Assembly.GetTypes()
         .Where(it => it.Namespace == "Che.Domain.Entity.ECountry") // 精确匹配命名空间
         .ToArray();

            // 修正2：添加表创建日志
            db.Aop.OnLogExecuting = (sql, pars) =>
            {
                Console.WriteLine(sql); // 打印生成的SQL
            };

            // 配置并创建表
            db.CodeFirst
              .SetStringDefaultLength(200)
              .InitTables(entityTypes);

            Console.WriteLine("表创建完成！");
            Console.ReadKey();
        }

    }
  
    
    }